﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Animation;

namespace Corona
{
	/// <summary>
	/// RelationLine.xaml 的交互逻辑
	/// </summary>
    
	public partial class RelationLine
	{
        //        Dictionary<string, TagIcon> editer = new Dictionary<string, TagIcon>();
        //        public TagIcon this[string str]{
        //            get { return editer[str]; }
        //            set { editer[str] = value; }
        //}


        /// <summary>
        /// 关系线的粗细
        /// </summary>
        public double RelationLineThickness
        {
            get { return this.RLine.StrokeThickness; }
            set { this.RLine.StrokeThickness = value; }
        }
        /// <summary>
        /// 线的起始位置
        /// </summary>
        public Point StartPoint
        {
            get { return (Point)GetValue(StartPointProperty); }
            set { SetValue(StartPointProperty, value); }
        }
        /// <summary>
        /// 线的结束位置
        /// </summary>
        public Point EndPoint
        {
            get { return (Point)GetValue(EndPointProperty); }
            set { SetValue(EndPointProperty, value);  }
        }
        /// <summary>
        /// 关系线开始的一段
        /// </summary>
        public Icon StartIcon;
        public static readonly DependencyProperty StartPointProperty = DependencyProperty.Register("StartPoint", typeof(Point), typeof(RelationLine), new FrameworkPropertyMetadata(StartPointPropertyChanged));
        public static readonly DependencyProperty EndPointProperty = DependencyProperty.Register("EndPoint", typeof(Point), typeof(RelationLine), new FrameworkPropertyMetadata(EndPointPropertyChanged));
        private static void StartPointPropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
        {
            (source as RelationLine).RLine.X1 = (source as RelationLine).StartPoint.X;
            (source as RelationLine).RLine.Y1 = (source as RelationLine).StartPoint.Y;
        }
        private static void EndPointPropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
        {
            (source as RelationLine).RLine.X2 = (source as RelationLine).EndPoint.X;
            (source as RelationLine).RLine.Y2 = (source as RelationLine).EndPoint.Y;
        }
        /// <summary>
        /// 关系线结束的一段
        /// </summary>
        public Icon EndIcon;
        /// <summary>
        /// 关系线的颜色
        /// </summary>
        public Color LineColor = Colors.Gold;

        private Storyboard ShowRLine = new Storyboard();

		private RelationLine()
		{
		}

        /// <summary>
        /// 创建关系线
        /// </summary>
        /// <param name="startIcon"></param>
        /// <param name="endIcon"></param>
        public RelationLine(Icon startIcon, Icon endIcon)
        {
            this.InitializeComponent();
            this.GetIcon(startIcon, endIcon);
            this.InitialLine();
        }
        public RelationLine(Icon startIcon, Icon endIcon, double linethickness)
        {
            this.InitializeComponent();
            this.GetIcon(startIcon, endIcon);
            this.InitialLine();
            this.RelationLineThickness = linethickness;
        }
        public RelationLine(Icon startIcon, Icon endIcon, Color clr)
        {
            this.InitializeComponent();
            this.GetIcon(startIcon, endIcon);
            this.InitialLine();
            this.LineColor = clr;
        }
        public RelationLine(Icon startIcon, Icon endIcon, double linethickness, Color clr)
        {
            this.InitializeComponent();
            this.GetIcon(startIcon, endIcon);
            this.InitialLine();
            this.LineColor = clr;
            this.RelationLineThickness = linethickness;
        }
        /// <summary>
        /// 获取关系线两端的图标
        /// </summary>
        /// <param name="startIcon"></param>
        /// <param name="endIcon"></param>
        private void GetIcon(Icon startIcon, Icon endIcon)
        {
            this.StartIcon = startIcon;
            this.EndIcon = endIcon;
        }
        /// <summary>
        /// 初始化
        /// </summary>
        private void InitialLine()
        {
            Point startPoint = new Point(StartIcon.Position.X, StartIcon.Position.Y);
            Point endPoint = new Point(EndIcon.Position.X, EndIcon.Position.Y);
            this.StartPoint = startPoint;
            this.EndPoint = endPoint;
            Canvas.SetZIndex(StartIcon, Canvas.GetZIndex(this) + 1);
            Canvas.SetZIndex(EndIcon, Canvas.GetZIndex(this) + 1);
            this.StartIcon.IconPositionChange += OnStartIconPositionChange;
            this.EndIcon.IconPositionChange += OnEndIconPositionChange;
            this.RLine.Stroke = new SolidColorBrush(LineColor);
            //this.RLine.StrokeDashArray = new DoubleCollection(new double[] { 4, 2 });
            ShowRLine = this.FindResource("OnLoaded") as Storyboard;
            ShowRLine.Begin(RLine);
        }
        /// <summary>
        /// 设置关系线开始位置
        /// </summary>
        public void SetStartPoint()
        {
            Point startPoint = new Point(StartIcon.Position.X, StartIcon.Position.Y);
            this.StartPoint = startPoint;
        }
        /// <summary>
        /// 设置关系线结束位置
        /// </summary>
        public void SetEndPoint()
        {
            Point endPoint = new Point(EndIcon.Position.X, EndIcon.Position.Y);
            this.EndPoint = endPoint;
        }
        private void OnStartIconPositionChange(object sender)
        {
            this.SetStartPoint();
        }
        private void OnEndIconPositionChange(object sender)
        {
            this.SetEndPoint();
        }
	}
}